package cn.icanci.rec.admin.biz.service.impl;

import cn.icanci.rec.admin.biz.mapper.DataSourceMapper;
import cn.icanci.rec.admin.biz.service.DataSourceService;
import cn.icanci.rec.admin.dal.mongodb.common.PageList;
import cn.icanci.rec.admin.dal.mongodb.daointerface.DataSourceDAO;
import cn.icanci.rec.admin.dal.mongodb.dateobject.DataSourceDO;
import cn.icanci.rec.common.model.config.DataSourceVO;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

/**
 * @author icanci
 * @since 1.0 Created in 2022/11/12 09:04
 */
@Service
public class DataSourceServiceImpl implements DataSourceService {
    @Resource
    private DataSourceDAO    dataSourceDAO;
    @Resource
    private DataSourceMapper dataSourceMapper;

    @Override
    public List<DataSourceVO> queryAll() {
        return dataSourceMapper.dos2vos(dataSourceDAO.queryAll());
    }

    @Override
    public void save(DataSourceVO dataSource) {
        if (doInsert(dataSource)) {
            dataSourceDAO.insert(dataSourceMapper.vo2do(dataSource));
        } else {
            dataSourceDAO.update(dataSourceMapper.vo2do(dataSource));
        }
    }

    @Override
    public DataSourceVO queryById(String id) {
        return dataSourceMapper.do2vo(dataSourceDAO.queryOneById(id));
    }

    @Override
    public PageList<DataSourceVO> queryPage(DataSourceVO dataSource, int pageNum, int pageSize) {
        PageList<DataSourceDO> pageQuery = dataSourceDAO.pageQuery(dataSourceMapper.vo2do(dataSource), pageNum, pageSize);
        return new PageList<>(dataSourceMapper.dos2vos(pageQuery.getData()), pageQuery.getPaginator());
    }
}
